﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace 泛微oa新建流程表单
{
    public class RSAEncryptPr

    {

        public static string RSAEncrypt(string data, string publicKey)
        {
            try
            {
                // 将Base64公钥转换为字节数组
                byte[] publicKeyBytes = Convert.FromBase64String(publicKey);

                using (RSA rsa = RSA.Create())
                {
                    // 导入X.509格式的公钥
                    rsa.ImportSubjectPublicKeyInfo(publicKeyBytes, out _);

                    // 加密数据，使用PKCS#1填充
                    byte[] dataBytes = Encoding.UTF8.GetBytes(data);
                    byte[] encryptedBytes = rsa.Encrypt(dataBytes, RSAEncryptionPadding.Pkcs1);

                    return Convert.ToBase64String(encryptedBytes);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"RSA加密失败: {ex.Message}");
                Console.WriteLine($"StackTrace: {ex.StackTrace}");
                return null;
            }
        }
    }
}
